package com.itheima.test;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;

/**
 * @author wmh
 * @version 1.0 2022/6/6
 * 使用jacob，提前有word/ppt/excel文档，转成pdf
 * @Description: Word转为Pdf
 **/
public class WordPdf {
    public static void main(String[] args) {
        String source = "D://test.docx";
        String target = "D://test.pdf";
        ActiveXComponent app = null;
        try {
            //调用windows中的office程序
            app = new ActiveXComponent("Word.Application");
            //调用word时不显示窗口
            app.setProperty("Visible",false);
            //获取所有word文档
            Dispatch docs = app.getProperty("Documents").toDispatch();
            //获取指定文件
            //打开文档
            Dispatch doc = Dispatch.call(docs, "Open",source).toDispatch();
            //调用文件的另存为功能--pdf的宏值17
            Dispatch.call(doc,"SaveAs",target,17);
            //关闭word
            Dispatch.call(doc,"Close");
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (app != null) {
                app.invoke("Quit", 0);
            }
        }

    }
}